﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VMC.Web.Logic;
using VMC.Domain.Model.Service;
using VMC.Domain.Model.Service.Abstract;
using VMC.Domain.Model.Account;
using System.Web.Security;

namespace VMC.Web.Controllers.User
{
    public class LogOnController : Controller
    {
        //
        // GET: /Login/
        private string ViewBasePath = "~/Views/User/";
        private IAccountModelService iAccount;
        public LogOnController() : this(new AccountModelService()) { }
        public LogOnController(IAccountModelService AccountService)
        {
            iAccount = AccountService;
        }

        public ActionResult Index()
        {
            return RedirectToAction("LogOn");
        }

        public ActionResult LogOn()
        {
            Response.Cookies.Remove("UserInformation");
            return View(ViewBasePath + "LogOn.cshtml");
        }

        [HttpPost]
        public ActionResult LogOn(LogOn Model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                //UNSOM.Domain.Entity.User mUser = new Domain.Entity.User();
                if (iAccount.ValidateLogin(Model))
                {
                    FormsAuthentication.SetAuthCookie(Model.UserName, Model.RememberMe);
                    SetUserCookie(Model.UserName);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
            return View(ViewBasePath + "LogOn.cshtml", Model);
        }
        
        private void SetUserCookie(string UserName)
        {
            HttpCookie UserInfo = new HttpCookie("UserInformation");
            UpdateProfile mUser = iAccount.GetUserProfile(UserName);
            UserInfo["UserName"] = UserName;
            UserInfo["FullName"] = mUser.FirstName + " " + mUser.LastName;
            UserInfo["UserID"] = mUser.ID.ToString();
            UserInfo.Expires = System.DateTime.Now.AddHours(2);
            Response.Cookies.Add(UserInfo);
        }

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            Response.Cookies.Remove("UserInformation");
            return RedirectToAction("Index", "Home");
        }


        public ActionResult Register()
        {
            return View(ViewBasePath + "Register.cshtml");
        }

        [HttpPost]
        public ActionResult Register(Register Model)
        {
            if (Model.Password != Model.ConfirmPassword)
            {
                ModelState.AddModelError("Password", "The password and confirmation password do not match.");
            }
            if (ModelState.IsValid)
            {
                if (iAccount.RegisterUser(Model))
                    return View(ViewBasePath + "Approval.cshtml");
                else
                    ModelState.MergeServiceErrors(iAccount.GetErrors());
            }
            return View(Model);           
        }


        

        //[HttpPost]
        //public ActionResult UpdateProfile(UpdateProfile Model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        if (iAccount.UpdateUserProfile(User.Identity.Name, Model))
        //        {
        //            SetUserCookie(User.Identity.Name);
        //            return View("UpdateProfileSuccess");
        //        }
        //        else
        //        {
        //            ModelState.MergeServiceErrors(iAccount.GetErrors());
        //            ModelState.AddModelError("", "Unable to update your profile.");
        //        }
        //    }
        //    return View(Model);
        //}

    }
}
